import * as Cesium from 'cesium/Cesium'
import AxisLinePrimitive from "./AxisLinePrimitive";
import TransformAxis from "./TransformAxis";
import getWidgetOrigin from "./getWidgetOrigin";

var widgetOriginScratch$2 = new Cesium.Cartesian3
    , originScratch$1 = new Cesium.Cartesian3
    , directionScratch$2 = new Cesium.Cartesian3
    , planeNormalScratch$1 = new Cesium.Cartesian3
    , pickedPointScratch$1 = new Cesium.Cartesian3
    , moveScratch$2 = new Cesium.Cartesian3
    , offsetProjectedScratch = new Cesium.Cartesian3
    , rayScratch$7 = new Cesium.Ray;
function getLinePrimitive(e) {
    return new AxisLinePrimitive({
        positions: [Cesium.Cartesian3.ZERO, TransformAxis.getValue(e)],
        arrow: !0,
        color: TransformAxis.getColor(e),
        id: e,
        show: !1
    })
}
function TranslationEditor(e) {
    var t = (e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT)).scene;
    this.originOffset = e.originOffset,
        this._polylineX = t.primitives.add(getLinePrimitive(TransformAxis.X)),
        this._polylineY = t.primitives.add(getLinePrimitive(TransformAxis.Y)),
        this._polylineZ = t.primitives.add(getLinePrimitive(TransformAxis.Z)),
        this._scene = t,
        this._canvas = t.canvas,
        this._setPositionCallback = e.setPosition,
        this._modelMatrix = new Cesium.Matrix4,
        this._fixedFrame = new Cesium.Matrix4,
        this._hpr = new Cesium.HeadingPitchRoll,
        this._dragAlongVector = void 0,
        this._offsetVector = new Cesium.Cartesian3,
        this._pickingPlane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X,0),
        this._dragging = !1,
        this._active = !1,
        this._transform = e.transform,
        this._radius = e.radius,
        this.update()
}
Cesium.defineProperties(TranslationEditor.prototype, {
    active: {
        get: function() {
            return this._active
        },
        set: function(e) {
            (this._active = e) ? (this._polylineX.show = !0,
                this._polylineY.show = !0,
                this._polylineZ.show = !0) : (this._polylineX.show = !1,
                this._polylineY.show = !1,
                this._polylineZ.show = !1,
                this._dragging = !1)
        }
    }
}),
    TranslationEditor.prototype.update = function() {
        var e = this._transform
            , t = this._scene.mapProjection.ellipsoid
            , i = Cesium.Matrix4.getTranslation(e, originScratch$1)
            , r = getWidgetOrigin(e, this.originOffset, widgetOriginScratch$2)
            , n = this._radius * Cesium.Matrix4.getMaximumScale(this._transform) * 1.5
            , a =Cesium. Transforms.fixedFrameToHeadingPitchRoll(this._transform, t, void 0, this._hpr);
        a.pitch = 0,
            a.roll = 0;
        var o = Cesium.Transforms.headingPitchRollToFixedFrame(i, a, t, void 0, this._fixedFrame);
        o = Cesium.Matrix4.setTranslation(o, r, o);
        var s = Cesium.Matrix4.multiplyByUniformScale(o, n, this._modelMatrix);
        this._polylineX.modelMatrix = s,
            this._polylineY.modelMatrix = s,
            this._polylineZ.modelMatrix = s
    }
    ,
    TranslationEditor.prototype.handleLeftDown = function(e) {
        for (var t, i = this._scene, r = i.camera, n = i.drillPick(e), a = 0; a < n.length; a++) {
            var o = n[a];
            if (Cesium.defined(o.id) && Cesium.defined(TransformAxis[o.id])) {
                t = o.id;
                break
            }
        }
        if (Cesium.defined(t)) {
            var s = Cesium.Matrix4.getTranslation(this._transform, originScratch$1)
                , l = TransformAxis.getValue(t)
                , c = Cesium.Matrix4.multiplyByPointAsVector(this._fixedFrame, l, directionScratch$2)
                , u = planeNormalScratch$1;
            u = .7 < Math.abs(Cesium.Cartesian3.dot(r.upWC, c)) ? Cesium.Cartesian3.cross(r.rightWC, c, u) : Cesium.Cartesian3.cross(r.upWC, c, u),
                Cesium.Cartesian3.normalize(u, u);
            var d = Cesium.Plane.fromPointNormal(s, u, this._pickingPlane)
                , h = Cesium.IntersectionTests.rayPlane(r.getPickRay(e, rayScratch$7), d, this._offsetVector);
            Cesium.defined(h) && (Cesium.Cartesian3.subtract(h, s, h),
                this._dragging = !0,
                this._dragAlongVector = l,
                i.screenSpaceCameraController.enableInputs = !1)
        }
    }
    ,
    TranslationEditor.prototype.handleMouseMove = function(e) {
        if (this._dragging) {
            var t = this._scene.camera
                , i = Cesium.IntersectionTests.rayPlane(t.getPickRay(e, rayScratch$7),
                this._pickingPlane, pickedPointScratch$1);
            if (Cesium.defined(i)) {
                var r = this._dragAlongVector
                    , n = Cesium.Matrix4.getTranslation(this._transform, originScratch$1)
                    , a = Cesium.Matrix4.multiplyByPointAsVector(this._fixedFrame, r, directionScratch$2)
                    , o = Cesium.Cartesian3.subtract(i, n, moveScratch$2);
                o = Cesium.Cartesian3.projectVector(o, a, o);
                var s = Cesium.Cartesian3.projectVector(this._offsetVector, a, offsetProjectedScratch);
                o = Cesium.Cartesian3.subtract(o, s, o),
                    n = Cesium.Cartesian3.add(n, o, n),
                    this._setPositionCallback(n)
            }
        }
    }
    ,
    TranslationEditor.prototype.handleLeftUp = function() {
        this._dragging = !1,
            this._scene.screenSpaceCameraController.enableInputs = !0
    }
    ,
    TranslationEditor.prototype.isDestroyed = function() {
        return !1
    }
    ,
    TranslationEditor.prototype.destroy = function() {
        this.active = !1;
        var e = this._scene;
        e.primitives.remove(this._polylineX),
            e.primitives.remove(this._polylineY),
            e.primitives.remove(this._polylineZ),
            Cesium.destroyObject(this)
    }
;
export default TranslationEditor;